package com.nhjf.admin.controller.system;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.admin.common.util.MD5;
import com.nhjf.admin.controller.BaseController;
import com.nhjf.admin.system.Constants;
import com.nhjf.admin.util.BuildIndexPageUtil;
import com.nhjf.admin.util.CheckPassword;
import com.nhjf.admin.util.Encrypt;
import com.nhjf.admin.util.Enums;
import com.nhjf.admin.util.Jurisdiction;
import com.nhjf.model.system.Resource;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.OperationLog;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.service.system.ResourceService;
import com.nhjf.service.system.RoleService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.OperationLogService;

import net.sf.json.JSONObject;

/**
 * @类功能说明：用户管理
 * @部门：技术部
 * @作者：龚一凡
 * @版本：V1.0
 */
@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController{

	@Autowired
	private RoleService roleService;
	@Autowired
	private ResourceService resourceService;
	@Autowired
	private OperationLogService operationLogService;

	@Autowired
	private UserService userService;


	/**
	 * 
	 * @方法功能说明：用户列表
	 * @修改者名字：龚一凡
	 * @修改内容：
	 * @参数：
	 * @return:
	 * @throws
	 */
	@RequestMapping(value = "/user_list")
	public String queryUserList(HttpServletRequest request, Model model,@RequestParam(value="page", defaultValue="1")Integer page){


		return "/system/query_user.html";
	}
	/**
	 * 修改当前登陆人密码
	 * @param curPwd
	 * @param newPwd
	 * @param conNewPwd
	 * @param userQo
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/modifyUserPwd")
	public String updatePwd(String curPwd,String newPwd,String conNewPwd,UserQo userQo,HttpServletRequest request,HttpServletResponse response) {
		try {
			User user=(User)Jurisdiction.getSession().getAttribute(com.nhjf.common.Constants.USER_INFO);
			userQo.setId(user.getId());
			userQo.setSeachRole(true);
			user= userService.queryUnique(userQo);
			if(curPwd.equals(user.getUserPassword())) {
				if(newPwd.equals(conNewPwd)) {
					user.setUserPassword(newPwd);
					userService.update(user);
					request.getSession().removeAttribute(Constants.SESSION_USER_KEY);
					Session session = Jurisdiction.getSession();
					session.removeAttribute(com.nhjf.common.Constants.USER_INFO);
					session.removeAttribute("resourceList");
					Cookie[] cookies = request.getCookies();//获取cookie
					for (Cookie cookie: cookies) {
						if ("loginToken".equals(cookie.getName())) {
							cookie.setMaxAge(0);
							cookie.setPath("/");
							response.addCookie(cookie);
						}
					}
					OperationLog operationLog=new OperationLog();
					operationLog.setOperationType(3);
					operationLog.setOperationDescription("修改当前登陆人密码!");
					operationLog.setUserId(user);
					operationLog.setOperationTime(new Date());
					operationLog.setOperationUserName(user.getUserName());
					operationLogService.save(operationLog);
					return "200";
				}else {
					return "501";
				}

			}else {
				return "502";
			}

		} catch (InvalidSessionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}





}
